import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wch/constant/resource_constant.dart';
import 'package:wch/model/report_list_entity.dart';
import 'package:wch/native/net_business.dart';
import 'package:wch/page/device_info.dart';
import 'package:wch/page/user_info.dart';
import 'package:wch/page/user_manager.dart';
import 'package:wch/utils/log_utils.dart';
import 'package:wch/utils/toast_utils.dart';
import 'package:wch/widget/common_widget.dart';
import 'package:wch/widget/padding_widget.dart';
import 'package:wch/widget/text_widget.dart';

import '../constant/event_bus_global.dart';
import '../model/acquisition_entity.dart';
import '../model/patient_edit_custom.dart';
import '../model/patient_info_custom.dart';
import '../model/patient_label.dart';
import '../model/patient_label_list.dart';
import '../widget/business_widget.dart';
import 'data_report.dart';

class UserDetailPage extends StatefulWidget {
  const UserDetailPage({super.key, required this.patientInfoCustom, required this.labelModel, this.labelList});

  // final String id;
  //
  // final String? name;
  // final String? sex;
  // final int? age;
  // final String? mobile;
  final PatientInfoCustom patientInfoCustom;
  final PatientLabelModel labelModel;
  final List<AcquisitionAcquisitionLabelList>? labelList;

  @override
  State<UserDetailPage> createState() => _UserDetailPageState();
}

class _UserDetailPageState extends State<UserDetailPage> with WidgetsBindingObserver{
  int selectedIndex = 0;

  PatientCustomModel patientEditModel = PatientCustomModel();

  ///是否第一次加载子页面
  bool isFirstLoadSubPage = true;
  ///键盘是否弹出
  bool isPopKeyBoard = false;

  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted) {
        if (MediaQuery.of(context).viewInsets.bottom == 0) {
          // 关闭键盘
          if (!isPopKeyBoard) {
            return;
          }
          setState(() {
            isPopKeyBoard = false;
          });
        } else {
          // 显示键盘
          if (isPopKeyBoard) {
            return;
          }
          setState(() {
            isPopKeyBoard = true;
          });
        }
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return CommonScaffold(
        title: '用户详情',
        body: KeyboardDismissOnTap(
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.all(15.w),
                height: 43.h,
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(21.5.h))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    _getTabItem('基本信息', ScreenUtil().screenWidth - 30.w / 3,
                        selectedIndex == 0, 0),
                    LineWidget(width: 0.5, height: 18.h),
                    _getTabItem('设备信息', ScreenUtil().screenWidth - 30.w / 3,
                        selectedIndex == 1, 1),
                    LineWidget(width: 0.5, height: 18.h),
                    _getTabItem('数据报告', ScreenUtil().screenWidth - 30.w / 3,
                        selectedIndex == 2, 2),
                  ],
                ),
              ),
              Expanded(child: Container(
                child: Stack(
                  children: _getTabContent(isPopKeyBoard),
                ),
              ),),

            ],
          ),
        ));
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  List<Widget> _getTabContent(bool _isPopKeyBoard) {
    return [
      Positioned(
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
        child: Offstage(
          offstage: selectedIndex != 0,
          child: UserInfoPage(id: widget.patientInfoCustom.userId, isPopKeyBoard: isPopKeyBoard,),
        ),
      ),
      Positioned(

        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
        child: Offstage(
          offstage: selectedIndex != 1,
          child: DeviceInfoPage(patientInfoCustom: widget.patientInfoCustom, labelModel: widget.labelModel, labelList: widget.labelList),
        ),
      ),
      Positioned(
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
        child: Offstage(
          offstage: selectedIndex != 2,
          child: DataReportPage(
            sickPersonId: int.parse(widget.patientInfoCustom.userId),
          ),
        ),
      ),
    ];
    // if (selectedIndex == 0) {
    //   // bool isFirst = isFirstLoadSubPage;
    //   // isFirstLoadSubPage = false;
    //   return UserInfoPage(id: widget.id, patientEditModel: patientEditModel);
    // } else if (selectedIndex == 1) {
    //   return DeviceInfoPage();
    // } else {
    //   return DataReportPage(
    //     users: ['张三', '李四', 'abc', '冈萨雷斯布拉沃', '123456'],
    //   );
    // }
  }

  Widget _getTabItem(String tab, double width, bool isSelected, int index) {
    return GestureDetector(
      onTap: () {
        setState(() {
          selectedIndex = index;
        });
      },
      child: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            F15TitleText(tab,
                color: isSelected ? themeColor : Color(0xFF424242)),
            Container(
              width: 24.w,
              height: 9.5.h,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(9.5.h / 2)),
                gradient: LinearGradient(
                  colors: isSelected
                      ? [Color(0x995A7BED), Color(0x005A7BED)]
                      : [Colors.transparent, Colors.transparent],
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                ),
                color: themeColor,
              ),
            ),
            VerPadding(4.h)
          ],
        ),
      ),
    );
  }
}
